<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" 
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>风控系统仪表板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .logo {
            display: flex;
            align-items: center;
            font-size: 1.5em;
            font-weight: bold;
        }
        
        .logo-icon {
            font-size: 1.8em;
            margin-right: 10px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.2em;
        }
        
        .nav-menu {
            background: white;
            border-bottom: 1px solid #ffe0e0;
            padding: 0 2rem;
        }
        
        .nav-content {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            gap: 30px;
        }
        
        .nav-item {
            padding: 15px 0;
            color: #ff6b6b;
            text-decoration: none;
            font-weight: 500;
            border-bottom: 3px solid transparent;
            transition: all 0.3s ease;
        }
        
        .nav-item:hover, .nav-item.active {
            color: #ee5a24;
            border-bottom-color: #ee5a24;
        }
        
        .main-content {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        
        .alert-banner {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
            color: white;
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .alert-icon {
            font-size: 2em;
        }
        
        .alert-content h2 {
            margin-bottom: 5px;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            transition: transform 0.3s ease;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .stat-card.danger {
            border-left: 4px solid #e74c3c;
        }
        
        .stat-card.warning {
            border-left: 4px solid #f39c12;
        }
        
        .stat-card.success {
            border-left: 4px solid #27ae60;
        }
        
        .stat-card.info {
            border-left: 4px solid #3498db;
        }
        
        .stat-icon {
            font-size: 2.5em;
            margin-bottom: 15px;
        }
        
        .stat-number {
            font-size: 2.2em;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .stat-number.danger {
            color: #e74c3c;
        }
        
        .stat-number.warning {
            color: #f39c12;
        }
        
        .stat-number.success {
            color: #27ae60;
        }
        
        .stat-number.info {
            color: #3498db;
        }
        
        .stat-label {
            color: #666;
            font-size: 1em;
        }
        
        .content-grid {
            display: grid;
            grid-template-columns: 2fr 1fr;
            gap: 30px;
        }
        
        .content-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
        }
        
        .card-title {
            font-size: 1.3em;
            color: #333;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .alert-item {
            padding: 15px 0;
            border-bottom: 1px solid #f0f2f5;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .alert-item:last-child {
            border-bottom: none;
        }
        
        .alert-level-high {
            color: #e74c3c;
            font-weight: bold;
        }
        
        .alert-level-medium {
            color: #f39c12;
            font-weight: bold;
        }
        
        .alert-level-low {
            color: #27ae60;
            font-weight: bold;
        }
        
        .risk-score {
            font-size: 3em;
            font-weight: bold;
            text-align: center;
            margin: 20px 0;
        }
        
        .risk-score.high {
            color: #e74c3c;
        }
        
        .risk-score.medium {
            color: #f39c12;
        }
        
        .risk-score.low {
            color: #27ae60;
        }
        
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 0.9em;
            transition: all 0.3s ease;
        }
        
        .btn-danger {
            background: #e74c3c;
            color: white;
        }
        
        .btn-danger:hover {
            background: #c0392b;
            transform: translateY(-1px);
        }
        
        .btn-warning {
            background: #f39c12;
            color: white;
        }
        
        .btn-warning:hover {
            background: #e67e22;
            transform: translateY(-1px);
        }
        
        .logout-btn {
            background: rgba(255,255,255,0.2);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            padding: 8px 16px;
            border-radius: 6px;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .logout-btn:hover {
            background: rgba(255,255,255,0.3);
            color: white;
        }
        
        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }
        
        .progress-bar {
            width: 100%;
            height: 10px;
            background: #ecf0f1;
            border-radius: 5px;
            overflow: hidden;
            margin-top: 10px;
        }
        
        .progress-fill {
            height: 100%;
            transition: width 0.3s ease;
        }
        
        .progress-fill.danger {
            background: #e74c3c;
        }
        
        .progress-fill.warning {
            background: #f39c12;
        }
        
        .progress-fill.success {
            background: #27ae60;
        }
        
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 15px;
            }
            
            .nav-content {
                flex-direction: column;
                gap: 10px;
            }
            
            .content-grid {
                grid-template-columns: 1fr;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- 头部 -->
    <header class="header">
        <div class="header-content">
            <div class="logo">
                <span class="logo-icon">🛡️</span>
                <span>风险控制系统</span>
            </div>
            <div class="user-info">
                <div class="user-avatar">
                    <span th:text="${#strings.substring(fullName != null ? fullName : username, 0, 1)}">U</span>
                </div>
                <div>
                    <div th:text="${fullName != null ? fullName : username}">用户名</div>
                    <div style="font-size: 0.9em; opacity: 0.8;" th:text="${email}">email@example.com</div>
                </div>
                <a href="/logout" class="logout-btn">退出登录</a>
            </div>
        </div>
    </header>
    
    <!-- 导航菜单 -->
    <nav class="nav-menu">
        <div class="nav-content">
            <a href="/dashboard" class="nav-item active">仪表板</a>
            <a href="/monitoring" class="nav-item" sec:authorize="hasAuthority('SCOPE_read') or hasAuthority('risk:read')">实时监控</a>
            <a href="/assessment" class="nav-item" sec:authorize="hasAuthority('SCOPE_write') or hasAuthority('risk:write')">风险评估</a>
            <a href="/management" class="nav-item" sec:authorize="hasAuthority('SCOPE_risk:manage') or hasAuthority('risk:manage')">风险管理</a>
            <a href="http://www.oauth2server.com:8080" class="nav-item" target="_blank">OA 系统</a>
        </div>
    </nav>
    
    <!-- 主要内容 -->
    <main class="main-content">
        <!-- 风险警报横幅 -->
        <section class="alert-banner">
            <div class="alert-icon">⚠️</div>
            <div class="alert-content">
                <h2>风险监控中心</h2>
                <p>实时监控系统风险状态，确保业务安全运行</p>
            </div>
        </section>
        
        <!-- 统计卡片 -->
        <section class="stats-grid">
            <div class="stat-card danger">
                <div class="stat-icon">🚨</div>
                <div class="stat-number danger" id="highRiskCount">-</div>
                <div class="stat-label">高风险事件</div>
            </div>
            <div class="stat-card warning">
                <div class="stat-icon">⚡</div>
                <div class="stat-number warning" id="mediumRiskCount">-</div>
                <div class="stat-label">中风险事件</div>
            </div>
            <div class="stat-card success">
                <div class="stat-icon">✅</div>
                <div class="stat-number success" id="lowRiskCount">-</div>
                <div class="stat-label">低风险事件</div>
            </div>
            <div class="stat-card info">
                <div class="stat-icon">📊</div>
                <div class="stat-number info" id="totalProcessed">-</div>
                <div class="stat-label">已处理事件</div>
            </div>
        </section>
        
        <!-- 内容区域 -->
        <section class="content-grid">
            <!-- 风险警报 -->
            <div class="content-card">
                <h2 class="card-title">
                    <span>🚨</span>
                    <span>最新风险警报</span>
                </h2>
                <div id="alertList" class="loading">正在加载...</div>
                <div style="margin-top: 20px;">
                    <button class="btn btn-warning" onclick="performAssessment()" sec:authorize="hasAuthority('SCOPE_write') or hasAuthority('risk:write')">
                        执行风险评估
                    </button>
                    <button class="btn btn-danger" onclick="handleRisk()" sec:authorize="hasAuthority('SCOPE_risk:manage') or hasAuthority('risk:manage')">
                        处理风险
                    </button>
                </div>
            </div>
            
            <!-- 风险评分 -->
            <div class="content-card">
                <h2 class="card-title">
                    <span>📊</span>
                    <span>风险评分</span>
                </h2>
                <div class="risk-score" id="riskScore">-</div>
                <div class="progress-bar">
                    <div class="progress-fill" id="riskProgress"></div>
                </div>
                <div style="margin-top: 20px; text-align: center; color: #666;">
                    <p>最后更新: <span id="updateTime">-</span></p>
                </div>
            </div>
        </section>
    </main>
    
    <script>
        // 加载风险警报
        async function loadAlerts() {
            try {
                const response = await fetch('/api/alerts');
                const result = await response.json();
                
                if (result.code === 200) {
                    const alerts = result.data.alerts;
                    
                    const alertList = document.getElementById('alertList');
                    alertList.innerHTML = alerts.map(alert => `
                        <div class="alert-item">
                            <div>
                                <div style="font-weight: 500;">${alert.type}</div>
                                <div style="font-size: 0.9em; color: #666; margin-top: 5px;">
                                    ${alert.description}
                                    ${alert.amount ? ' - 金额: ¥' + alert.amount.toLocaleString() : ''}
                                    ${alert.location ? ' - 地点: ' + alert.location : ''}
                                    ${alert.count ? ' - 次数: ' + alert.count : ''}
                                </div>
                                <div style="font-size: 0.8em; color: #999; margin-top: 5px;">
                                    ${new Date(alert.createTime).toLocaleString()}
                                </div>
                            </div>
                            <div class="alert-level-${alert.level === '高' ? 'high' : alert.level === '中' ? 'medium' : 'low'}">
                                ${alert.level}
                            </div>
                        </div>
                    `).join('');
                } else {
                    document.getElementById('alertList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
                }
            } catch (error) {
                console.error('加载风险警报失败:', error);
                document.getElementById('alertList').innerHTML = '<div style="color: #e74c3c;">加载失败</div>';
            }
        }
        
        // 加载统计数据
        async function loadStatistics() {
            try {
                const response = await fetch('/api/statistics');
                const result = await response.json();
                
                if (result.code === 200) {
                    const data = result.data;
                    
                    document.getElementById('highRiskCount').textContent = data.highRiskCount;
                    document.getElementById('mediumRiskCount').textContent = data.mediumRiskCount;
                    document.getElementById('lowRiskCount').textContent = data.lowRiskCount;
                    document.getElementById('totalProcessed').textContent = data.totalProcessed;
                    
                    // 更新风险评分
                    const riskScore = Math.round(data.riskScore);
                    document.getElementById('riskScore').textContent = riskScore;
                    document.getElementById('updateTime').textContent = new Date(data.updateTime).toLocaleString();
                    
                    // 设置风险评分颜色和进度条
                    const scoreElement = document.getElementById('riskScore');
                    const progressElement = document.getElementById('riskProgress');
                    
                    if (riskScore >= 80) {
                        scoreElement.className = 'risk-score high';
                        progressElement.className = 'progress-fill danger';
                    } else if (riskScore >= 50) {
                        scoreElement.className = 'risk-score medium';
                        progressElement.className = 'progress-fill warning';
                    } else {
                        scoreElement.className = 'risk-score low';
                        progressElement.className = 'progress-fill success';
                    }
                    
                    progressElement.style.width = riskScore + '%';
                } else {
                    console.error('加载统计数据失败:', result.message);
                }
            } catch (error) {
                console.error('加载统计数据失败:', error);
            }
        }
        
        // 执行风险评估
        async function performAssessment() {
            try {
                const response = await fetch('/api/assess');
                const result = await response.json();
                
                if (result.code === 200) {
                    const assessment = result.data;
                    alert(`风险评估完成！\n\n评估ID: ${assessment.assessmentId}\n风险等级: ${assessment.riskLevel}\n风险评分: ${Math.round(assessment.riskScore)}\n评估时间: ${new Date(assessment.assessTime).toLocaleString()}`);
                    
                    // 刷新数据
                    loadStatistics();
                    loadAlerts();
                } else {
                    alert('风险评估失败: ' + result.message);
                }
            } catch (error) {
                console.error('执行风险评估失败:', error);
                alert('执行风险评估失败，请稍后重试');
            }
        }
        
        // 处理风险
        async function handleRisk() {
            try {
                const response = await fetch('/api/handle');
                const result = await response.json();
                
                if (result.code === 200) {
                    alert('风险处理任务创建成功！\n\n' + result.data);
                    
                    // 刷新数据
                    loadStatistics();
                    loadAlerts();
                } else {
                    alert('创建风险处理任务失败: ' + result.message);
                }
            } catch (error) {
                console.error('处理风险失败:', error);
                alert('处理风险失败，请稍后重试');
            }
        }
        
        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            loadAlerts();
            loadStatistics();
            
            // 每15秒刷新一次数据
            setInterval(() => {
                loadAlerts();
                loadStatistics();
            }, 15000);
        });
        
        // 导航菜单激活状态
        document.querySelectorAll('.nav-item').forEach(item => {
            if (item.href === window.location.href) {
                item.classList.add('active');
            }
        });
    </script>
</body>
</html> 